﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Controls
{
    public class PGroupBox : GroupBox
    {
        private Color _textColor = Color.Black;         //边框文本颜色
        private Color _borderColor = Color.LightGray;   //边框颜色

        [Browsable(true)]
        [Description("分组框标题颜色")]
        [Category("Appearance")]
        public Color TextColor
        {
            get { return _textColor; }
            set { _textColor = value; this.Invalidate(); }
        }

        [Browsable(true)]
        [Description("边框颜色")]
        [Category("Appearance")]
        public Color BorderColor
        {
            get { return _borderColor; }
            set { _borderColor = value; this.Invalidate(); }
        }


        public PGroupBox() : base()
        {
            base.SetStyle(ControlStyles.UserPaint |             // 控件将自行绘制，而不是通过操作系统来绘制  
                    ControlStyles.OptimizedDoubleBuffer |       // 该控件首先在缓冲区中绘制，而不是直接绘制到屏幕上，这样可以减少闪烁  
                    ControlStyles.AllPaintingInWmPaint |        // 控件将忽略 WM_ERASEBKGND 窗口消息以减少闪烁  
                    ControlStyles.ResizeRedraw |                // 在调整控件大小时重绘控件  
                    ControlStyles.SupportsTransparentBackColor, // 控件接受 alpha 组件小于 255 的 BackColor 以模拟透明  
                    true);

            base.UpdateStyles();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            e.Graphics.Clear(BackColor);
            Pen border = new Pen(_borderColor);
            e.Graphics.DrawString(Text, Font, new SolidBrush(_textColor), 10,1);
            e.Graphics.DrawLine(border, 1, 7, 8, 7);
            e.Graphics.DrawLine(border, e.Graphics.MeasureString(this.Text, this.Font).Width + 8, 7, this.Width - 2, 7);
            e.Graphics.DrawLine(border, 1, 7, 1, this.Height - 2);
            e.Graphics.DrawLine(border, 1, this.Height - 2, this.Width - 2, this.Height - 2);
            e.Graphics.DrawLine(border, this.Width - 2, 7, this.Width - 2, this.Height - 2);
        }
    }
}
